
package com.wyzz.hr.bo.impl;

import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.wyzz.hr.bo.IMachineBO;
import com.wyzz.hr.pojo.AttdMachine;
import com.wyzz.hr.pojo.AttdSyncRecord;
import com.wyzz.hr.pojo.Attdoriginaldata;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Machine;
import com.wyzz.hr.util.DateUtil;

import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.eclipse.birt.report.model.api.util.StringUtil;

public class MachineBOImpl implements IMachineBO {

	public MachineBOImpl() {
		currMac = null;
		isTFTMachine = false;
	}

	public int getSuccCode() {
		return SUCC_CODE;
	}

	public int getErrorCode() {
		Variant errorCode = new Variant(Integer.valueOf(1), true);
		Dispatch.call(getComObjOri(), "GetLastError", errorCode);
		return ((Integer) errorCode.toJavaObject()).intValue();
	}

	public int connectNet(AttdMachine machine) {
		if (checkMachineConnect(machine))
			return 22;
		Variant rs;
		if (machine.getMacPassword().intValue() != 0) {
			rs = Dispatch.call(getComObjOri(), "SetCommPassword", machine
					.getMacPassword());
			if (!rs.getBoolean())
				return getErrorCode();
		}
		if (StringUtil.isEmpty(machine.getMacPort())
				|| StringUtil.isEmpty(machine.getMacIP()))
			return 4;
		rs = Dispatch.call(getComObjOri(), "connect_Net", machine.getMacIP(),
				Integer.valueOf(Integer.parseInt(machine.getMacPort())));
		if (!rs.getBoolean()) {
			return getErrorCode();
		} else {
			Map machineStatusMap = Machine.machineSatusMap;
			machineStatusMap.put(machine.getMacId(), new Boolean(true));
			setCurrMac(machine);
			isTFTMachine = isTFTMachine();
			return getSuccCode();
		}
	}

	public void disConnectNet() {
		Dispatch.call(getComObjOri(), "Disconnect");
		Machine.machineSatusMap.put(getCurrMac().getMacId(), Boolean
				.valueOf(false));
	}

	public int addOrModifyUser(AttdSyncRecord rec) {
		String functionUser = "";
		String functionTemp = "";
		if (!isTFTMachine) {
			functionUser = "setUserInfo";
			functionTemp = "SetUserTmpStr";
		} else {
			functionUser = "SSR_SetUserInfo";
			functionTemp = "SSR_SetUserTmpStr";
		}
		Variant rs;
		if (!StringUtils.isEmpty(rec.getAsrEmpCardNo())) {
			rs = Dispatch.call(getComObjOri(), "SetStrCardNumber", rec
					.getAsrEmpCardNo());
			if (!rs.getBoolean())
				return getErrorCode();
		}
		rs = Dispatch.call(getComObjOri(), functionUser, Integer
				.valueOf(DEFAULT_MACHINE_NO), rec.getAsrEmpMachineNo(), rec
				.getAsrEmp().getEmpName(), rec.getPassword(), rec
				.getAsrAuthLevel(), Integer.valueOf(1));
		if (getCurrMac().getMacHasFinger().intValue() == 1) {
			String tempArr[] = rec.getTempArr();
			if (tempArr != null && tempArr[0] != null) {
				for (int i = 0; i < tempArr.length
						&& !StringUtils.isEmpty(tempArr[i]); i++)
					rs = Dispatch.call(getComObjOri(), functionTemp, Integer
							.valueOf(DEFAULT_MACHINE_NO),
							isTFTMachine ? ((Object) ((new StringBuilder())
									.append(rec.getAsrEmpMachineNo())
									.append("").toString())) : ((Object) (rec
									.getAsrEmpMachineNo())),
							Integer.valueOf(i), tempArr[i]);

			}
		}
		return getErrorCode();
	}

	public List getAllUserInfo() {
		List recList = new ArrayList();
		Variant rs = Dispatch.call(getComObjOri(), "ReadAllUserID", Integer
				.valueOf(DEFAULT_MACHINE_NO));
		if (!rs.getBoolean())
			return recList;
		if (getCurrMac().getMacHasFinger().intValue() == 1)
			Dispatch.call(getComObjOri(), "ReadAllTemplate", Integer
					.valueOf(DEFAULT_MACHINE_NO));
		Variant enrollNo = null;
		Variant name = new Variant("", true);
		Variant pass = new Variant("", true);
		Variant priv = new Variant(Integer.valueOf(0), true);
		Variant enabled = new Variant(Boolean.valueOf(true), true);
		Variant cardNo = new Variant("", true);
		Variant tempStr = new Variant("", true);
		Variant tempStrLength = new Variant(Integer.valueOf(0), true);
		String functionUser = null;
		String functionTemp = null;
		if (!isTFTMachine) {
			functionUser = "GetAllUserInfo";
			functionTemp = "GetUserTmpStr";
			enrollNo = new Variant(Integer.valueOf(0), true);
		} else {
			functionUser = "SSR_GetAllUserInfo";
			functionTemp = "SSR_GetUserTmpStr";
			enrollNo = new Variant("", true);
		}
		do {
			rs = Dispatch.call(getComObjOri(), functionUser, Integer
					.valueOf(DEFAULT_MACHINE_NO), enrollNo, name, pass, priv,
					enabled);
			if (rs.getBoolean()) {
				Dispatch.call(getComObjOri(), "GetStrCardNumber", cardNo);
				String tempArr[] = new String[10];
				if (getCurrMac().getMacHasFinger().intValue() == 1) {
					int i = 0;
					do {
						if (i > 9)
							break;
						rs = Dispatch.call(getComObjOri(), functionTemp,
								Integer.valueOf(DEFAULT_MACHINE_NO), enrollNo,
								Integer.valueOf(i), tempStr, tempStrLength);
						if (!rs.getBoolean()
								|| ((Integer) tempStrLength.toJavaObject())
										.intValue() == 0)
							break;
						tempArr[i] = (String) tempStr.toJavaObject();
						i++;
					} while (true);
				}
				AttdSyncRecord rec = new AttdSyncRecord();
				Employee emp = new Employee();
				rec.setAsrEmp(emp);
				if (!isTFTMachine)
					rec.setAsrEmpMachineNo((Integer) enrollNo.toJavaObject());
				else
					rec.setAsrEmpMachineNo(Integer.valueOf(Integer
							.parseInt((String) enrollNo.toJavaObject())));
				rec.setAsrEmpCardNo((String) cardNo.toJavaObject());
				emp.setEmpName((String) name.toJavaObject());
				rec.setPassword((String) pass.toJavaObject());
				rec.setAsrAuthLevel((Integer) priv.toJavaObject());
				rec.setTempArr(tempArr);
				recList.add(rec);
			} else {
				return recList;
			}
		} while (true);
	}

	public AttdSyncRecord getOneUserInfo(int empMachineNo) {
		Variant name = new Variant("", true);
		Variant pass = new Variant("", true);
		Variant priv = new Variant(Integer.valueOf(0), true);
		Variant enabled = new Variant(Boolean.valueOf(true), true);
		Variant cardNo = new Variant("", true);
		Variant tempStr = new Variant("", true);
		Variant tempStrLength = new Variant(Integer.valueOf(0), true);
		String functionUser = null;
		String functionTemp = null;
		if (!isTFTMachine) {
			functionUser = "GetUserInfo";
			functionTemp = "GetUserTmpStr";
		} else {
			functionUser = "SSR_GetUserInfo";
			functionTemp = "SSR_GetUserTmpStr";
		}
		Variant rs = Dispatch.call(getComObjOri(), functionUser, Integer
				.valueOf(DEFAULT_MACHINE_NO),
				isTFTMachine ? ((Object) ((new StringBuilder()).append(
						empMachineNo).append("").toString()))
						: ((Object) (Integer.valueOf(empMachineNo))), name,
				pass, priv, enabled);
		rs = Dispatch.call(getComObjOri(), "GetStrCardNumber", cardNo);
		String tempArr[] = new String[10];
		if (getCurrMac().getMacHasFinger().intValue() == 1) {
			int i = 0;
			do {
				if (i > 9)
					break;
				rs = Dispatch.call(getComObjOri(), functionTemp, Integer
						.valueOf(DEFAULT_MACHINE_NO),
						isTFTMachine ? ((Object) ((new StringBuilder()).append(
								empMachineNo).append("").toString()))
								: ((Object) (Integer.valueOf(empMachineNo))),
						Integer.valueOf(i), tempStr, tempStrLength);
				if (!rs.getBoolean()
						|| ((Integer) tempStrLength.toJavaObject()).intValue() == 0)
					break;
				tempArr[i] = (String) tempStr.toJavaObject();
				i++;
			} while (true);
		}
		Employee emp = new Employee();
		emp.setEmpName((String) name.toJavaObject());
		AttdSyncRecord rec = new AttdSyncRecord();
		rec.setAsrEmpMachineNo(Integer.valueOf(empMachineNo));
		rec.setAsrEmpCardNo((String) cardNo.toJavaObject());
		rec.setAsrEmp(emp);
		rec.setPassword((String) pass.toJavaObject());
		rec.setAsrAuthLevel((Integer) priv.toJavaObject());
		rec.setTempArr(tempArr);
		return rec;
	}

	public int deleteUser(AttdSyncRecord rec) {
		Variant rs;
		if (!isTFTMachine) {
			rs = Dispatch.call(getComObjOri(), "DeleteEnrollData", Integer
					.valueOf(DEFAULT_MACHINE_NO), rec.getAsrEmpMachineNo(),
					Integer.valueOf(DEFAULT_MACHINE_NO), Integer.valueOf(0));
		} else {
			rs = Dispatch.call(getComObjOri(), "SSR_DeleteEnrollData", Integer
					.valueOf(DEFAULT_MACHINE_NO), (new StringBuilder()).append(
					rec.getAsrEmpMachineNo()).append("").toString(), Integer
					.valueOf(0));
			if (getErrorCode() != 0 && !rs.getBoolean())
				return getErrorCode();
		}
		return getSuccCode();
	}

	public int refreshData() {
		Variant rs = Dispatch.call(getComObjOri(), "RefreshData", Integer
				.valueOf(DEFAULT_MACHINE_NO));
		if (!rs.getBoolean())
			return getErrorCode();
		else
			return getSuccCode();
	}

	public List getAllCardData(List asrList, Date dateFrom, Date dateTo) {
		Variant dwTMachineNumber = new Variant(Integer.valueOf(0), true);
		Variant dwEMachineNumber = new Variant(Integer.valueOf(0), true);
		Variant enrollNoBW = new Variant(Integer.valueOf(0), true);
		Variant enrollNoTFT = new Variant("", true);
		Variant verifyMode = new Variant(Integer.valueOf(0), true);
		Variant inOutMode = new Variant(Integer.valueOf(0), true);
		Variant year = new Variant(Integer.valueOf(0), true);
		Variant month = new Variant(Integer.valueOf(0), true);
		Variant day = new Variant(Integer.valueOf(0), true);
		Variant hour = new Variant(Integer.valueOf(0), true);
		Variant minute = new Variant(Integer.valueOf(0), true);
		Variant second = new Variant(Integer.valueOf(0), true);
		Variant workcode = new Variant(Integer.valueOf(0), true);
		List origDataList = new ArrayList();
		Variant rs = Dispatch.call(getComObjOri(), "ReadGeneralLogData",
				Integer.valueOf(DEFAULT_MACHINE_NO));
		if (!rs.getBoolean())
			return origDataList;
		Map empMap = asrListToEmpMap(asrList);
		String function = null;
		Object args[] = null;
		if (!isTFTMachine) {
			function = "GetGeneralLogData";
			args = (new Object[] { Integer.valueOf(DEFAULT_MACHINE_NO),
					dwTMachineNumber, enrollNoBW, dwEMachineNumber, verifyMode,
					inOutMode, year, month, day, hour, minute });
		} else {
			function = "SSR_GetGeneralLogData";
			args = (new Object[] { Integer.valueOf(DEFAULT_MACHINE_NO),
					enrollNoTFT, verifyMode, inOutMode, year, month, day, hour,
					minute, second, workcode });
		}
		Map aodIdMap = new HashMap();
		do {
			rs = Dispatch.callN(getComObjOri(), function, args);
			if (!rs.getBoolean())
				break;
			Integer empMachineNo;
			if (!isTFTMachine)
				empMachineNo = (Integer) enrollNoBW.toJavaObject();
			else
				empMachineNo = Integer.valueOf(Integer
						.parseInt((String) enrollNoTFT.toJavaObject()));
			Employee emp = (Employee) empMap.get(empMachineNo);
			if (emp != null) {
				Attdoriginaldata data = new Attdoriginaldata();
				Date aodCardTime = DateUtil.parseDateTimeToD(((Integer) year
						.toJavaObject()).intValue(), ((Integer) month
						.toJavaObject()).intValue(), ((Integer) day
						.toJavaObject()).intValue(), ((Integer) hour
						.toJavaObject()).intValue(), ((Integer) minute
						.toJavaObject()).intValue(), (new Integer(0))
						.intValue());
				String key = (new StringBuilder()).append(emp.getId()).append(
						DateUtil.formatDateToS(aodCardTime, "yyyyMMddHHmmss"))
						.toString();
				if (!aodIdMap.containsKey(key)
						&& (dateFrom == null || aodCardTime.compareTo(dateFrom) >= 0)
						&& (dateTo == null || aodCardTime.compareTo(dateTo) <= 0)) {
					aodIdMap.put(key, key);
					data.setAodId(key);
					data.setAodCardTime(aodCardTime);
					data.setAodAttdDate(DateUtil
							.convDateTimeToDate(aodCardTime));
					data.setAttdEmp(emp);
					data.setAodEmpDistinctNo(emp.getEmpDistinctNo());
					data.setAodStatus(Integer.valueOf(2));
					origDataList.add(data);
				}
			}
		} while (true);
		return origDataList;
	}

	public boolean syncMachineTime() {
		Variant rs = Dispatch.call(getComObjOri(), "SetDeviceTime", Integer
				.valueOf(DEFAULT_MACHINE_NO));
		return rs.getBoolean();
	}

	public int clearRecord() {
		Variant rs = Dispatch.call(getComObjOri(), "ClearGLog", Integer
				.valueOf(DEFAULT_MACHINE_NO));
		if (!rs.getBoolean())
			return getErrorCode();
		else
			return getSuccCode();
	}

	public Dispatch getComObjOri() {
		if (comObjOri == null)
			comObjOri = new Dispatch("zkemkeeper.ZKEM");
		return comObjOri;
	}

	public String getErrorByCode(int errorCode) {
		String msg = "";
		switch (errorCode) {
		case -100:
			msg = "\u4E0D\u652F\u6301\u6216\u6570\u636E\u4E0D\u5B58\u5728\uFF01";
			break;

		case -7:
			msg = "\u547D\u4EE4\u672A\u5F97\u5230\u54CD\u5E94\uFF01";
			break;

		case -6:
			msg = "\u8FDE\u63A5\u672A\u901A\u8FC7\u8BA4\u8BC1\uFF0C\u6709\u53EF\u80FD\u6709\u5BC6\u7801\uFF01";
			break;

		case -5:
			msg = "\u521B\u5EFA\u8BFB\u7EBF\u7A0B\u5931\u8D25\uFF01";
			break;

		case -4:
			msg = "\u7A7A\u95F4\u4E0D\u8DB3\uFF01";
			break;

		case -3:
			msg = "\u9519\u8BEF\u7684\u5927\u5C0F\uFF01";
			break;

		case -2:
			msg = "bind \u7AEF\u53E3\u5931\u8D25\uFF01";
			break;

		case -1:
			msg = "\u52A0\u8F7D\u901A\u8BAF\u652F\u6301\u5E93\u5931\u8D25\uFF01";
			break;

		case 0: // '\0'
			msg = "\u627E\u4E0D\u5230\u6570\u636E\u6216\u91CD\u590D\u6570\u636E\uFF01";
			break;

		case 1: // '\001'
			msg = "\u64CD\u4F5C\u6B63\u786E\uFF01";
			break;

		case 4: // '\004'
			msg = "\u53C2\u6570\u9519\u8BEF\uFF01";
			break;

		case 21: // '\025'
			msg = "\u975E\u6307\u7EB9\u673A\uFF0C\u4E0D\u80FD\u8FDB\u884C\u6307\u7EB9\u62F7\u8D1D\u64CD\u4F5C\uFF01";
			break;

		case 22: // '\026'
			msg = "\u5F53\u524D\u8003\u52E4\u673A\u6B63\u5728\u88AB\u8BBF\u95EE\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5\uFF01";
			break;

		default:
			msg = "\u672A\u77E5\u9519\u8BEF\uFF01";
			break;
		}
		return msg;
	}

	private Map asrListToEmpMap(List list) {
		Map map = new HashMap();
		AttdSyncRecord record;
		for (Iterator i$ = list.iterator(); i$.hasNext(); map.put(Integer
				.valueOf(Integer.parseInt(record.getAsrEmpMachineNo()
						.toString())), record.getAsrEmp()))
			record = (AttdSyncRecord) i$.next();

		return map;
	}

	private boolean isTFTMachine() {
		Variant rs = Dispatch.call(getComObjOri(), "IsTFTMachine", Integer
				.valueOf(DEFAULT_MACHINE_NO));
		return rs.getBoolean();
	}

	public boolean checkMachineConnect(AttdMachine machine) {
		Map machineStatusMap = Machine.machineSatusMap;
		Boolean status = (Boolean) machineStatusMap.get(machine.getMacId());
		return status != null && status.booleanValue();
	}

	public AttdMachine getCurrMac() {
		return currMac;
	}

	public void setCurrMac(AttdMachine currMac) {
		this.currMac = currMac;
	}

	private Dispatch comObjOri;
	private AttdMachine currMac;
	private boolean isTFTMachine;
	public static int SUCC_CODE = 1;
	public static int DEFAULT_MACHINE_NO = 1;

}
